package pers.litao.bit.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import pers.litao.bit.commons.entities.user.UserInfoEntity;
import pers.litao.bit.commons.utils.ClientInfo;
import pers.litao.bit.commons.utils.R;
import pers.litao.bit.commons.utils.SysUtil;
import pers.litao.bit.commons.validator.group.RegGroup;
import pers.litao.bit.commons.validator.group.UpdatePassword;
import pers.litao.bit.commons.vo.user.UserRegisterVo;
import pers.litao.bit.user.service.UserInfoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 李涛
 * @date 2020-11-16 15:49
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Value("${li.profiles}")
    private String profiles;

    /**
     * 用户注册
     *
     * @param userInfoEntity
     * @return
     */
    @RequestMapping(value = "/userinfo/register",method = RequestMethod.POST)
    public R sellerRegister(@Validated({RegGroup.class}) @RequestBody UserRegisterVo userInfoEntity){
        return userInfoService.userSave(userInfoEntity);
    }

    /**
     * 更改密码
     *
     * @param userInfoEntity
     * @param request
     * @return
     */
    @RequestMapping(value = "/userinfo/updatePassword",method = RequestMethod.POST)
    public R updatePassword(@Validated({UpdatePassword.class}) @RequestBody UserRegisterVo userInfoEntity,HttpServletRequest request){

        return userInfoService.updatePassword(userInfoEntity, SysUtil.getLoginUserInfo(request));
    }

    /**
     * 用户登录
     *
     * @param userInfoEntity
     * @return
     */
    @RequestMapping(value = "/userinfo/login",method = RequestMethod.POST)
    public R sellerLogin( @RequestBody UserInfoEntity userInfoEntity){
        return userInfoService.userLogin(userInfoEntity);
    }

    /**
     * 查询用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/userinfo/getuser/{id}",method = RequestMethod.GET)
    public R getUser(@PathVariable("id") Long id, HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        String service = httpServletRequest.getHeader("service");
        return userInfoService.getUserInfo(token,service,id);
    }

    /**
     * 获取用户列表，内部调用
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/userinfo/list",method = RequestMethod.POST)
    public R getUserList(@RequestBody Long[] ids){

        return userInfoService.getUserList(ids);
    }

    /**
     * 获取地级市相同的用户
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/userinfo/getUserIds",method = RequestMethod.POST)
    public R getUserIds(@RequestBody  Long id){
        return userInfoService.getUserIds(id);
    }

    /**
     * 获取所有用户，内部调用
     *
     * @return
     */
    @RequestMapping(value = "/userinfo/getUserAll")
    public R getUserAll(){
        return R.ok(userInfoService.list());
    }
    /**
     * 测试环境
     *
     * @return
     */
    @RequestMapping(value = "/userinfo/profiles")
    public R testProfiles(){
        return R.ok(profiles);
    }
}
